﻿using DeviceCommons.DataHandling;
namespace DeviceCommons.DeviceMessages.Models.V1
{
    public class DeviceMessageInfoReadingStates : IDeviceMessageInfoReadingStates
    {

        public byte Count => (byte)(StateArray?.Length ?? 0);

        public IDeviceMessageInfoReadingState[]? StateArray { get; set; }

        public int DataLength => 1 + (StateArray?.Sum(x => x.DataLength) ?? 0);

        public void Dispose()
        {
            if (StateArray != null && StateArray.Length > 0)
            {
                foreach (var state in StateArray)
                {
                    state?.Dispose();
                }
                // 只清理数组内容，不返回到内存池
                // 因为StateArray通常不是从内存池租借的（通过new IDeviceMessageInfoReadingState[]创建）
                Array.Clear(StateArray, 0, StateArray.Length);
                StateArray = [];
            }
            GC.SuppressFinalize(this);
        }
    }
}
